package com.codefactory.generator.utils;

import com.codefactory.generator.model.TableColumn;
import com.codefactory.generator.model.TableInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class TableColumnUtil {

    public static List<TableColumn> parseColumn(List<TableColumn> columnList) throws Exception {
        List<TableColumn> newColumnList = new ArrayList<>(columnList.size());
        for (TableColumn tableColumn : columnList) {
            TableColumn column = new TableColumn();
            BeanUtils.copyProperties(tableColumn, column);

            String columnName = column.getColumnName();
            String orgColumnType = column.getColumnType();
            String varName = CaseUtil.tableNameToVarName(columnName);
            String className = column.getColumnClassName();
            String columnType = TableColumnUtil.fixColumnType(orgColumnType);
            String varType = TableColumnUtil.columnToJavaType(columnType);
            if (varType == null) {
                String msg = String.format("存在未知类型！%s, 默认类型 %s ", orgColumnType, className);
                throw new Exception(msg);
            }
            //
            column.setColumnType(columnType);
            column.setVarName(varName);
            column.setVarType(varType);

            newColumnList.add(column);
        }

        //设置从VO中排除
        for (TableColumn column : newColumnList) {
            switch (column.getVarName()) {
                case "createdBy"://createBy
                case "createdAt"://createDate
                case "updatedBy"://updateBy
                case "updatedAt"://updateDate
                case "deleteFlag"://"stateFlag"
                case "createDate"://createDate
                case "updateDate"://updateDate
                    column.setExcludeFromVO(true);
                    break;
                default:
                    column.setExcludeFromVO(false);
                    break;
            }
        }
        return newColumnList;
    }


    public static String fixColumnType(String dataType) {
        String newDataType;
        switch (dataType) {
            case "VARCHAR":
            case "VARCHAR2":
            case "CHAR":
            case "TEXT":
            case "JSON":
            case "LONGTEXT":
                newDataType = "VARCHAR";
                break;
            case "DATE":
            case "DATETIME":
            case "TIMESTAMP":
                newDataType = "TIMESTAMP";
                break;
            case "INT":
            case "SMALLINT":
            case "MEDIUMINT":
            case "INTEGER":
                newDataType = "INTEGER";
                break;
            default:
                newDataType = dataType;
                break;
        }
        return newDataType;
    }

    public static String columnToJavaType(String dataBaseType) {
        String javaType;
        switch (dataBaseType) {
            case "VARCHAR":
            case "VARCHAR2":
            case "CHAR":
            case "TEXT":
            case "JSON":
            case "LONGTEXT":
                javaType = "String";
                break;
            case "DATE":
            case "DATETIME":
            case "TIMESTAMP":
                javaType = "Date";
                break;
            case "INT":
            case "SMALLINT":
            case "MEDIUMINT":
            case "INTEGER":
                javaType = "Integer";
                break;
            case "BIGINT":
                javaType = "Long";
                break;
            case "TINYINT":
                javaType = "Byte";
                break;
            case "FLOAT":
                javaType = "Float";
                break;
            case "DOUBLE":
                javaType = "Double";
                break;
            case "DECIMAL":
                javaType = "BigDecimal";
                break;
            case "BIT":
                javaType = "Boolean";
                break;
            default:
                javaType = null;
                break;
        }
        return javaType;
    }

    /**
     * 获取表信息
     *
     * @param tableName
     * @param modelName
     * @return
     * @throws Exception
     */
    public static TableInfo getTableInfo(String tableName, String tableComment, String modelName) {
        modelName = StringUtils.isEmpty(modelName) ? tableName : modelName;
        //
        TableInfo tableInfo = new TableInfo();
        if (StringUtils.isNotBlank(tableName)) tableInfo.setTableName(tableName.toLowerCase());
        tableInfo.setTableComment(tableComment);
        tableInfo.setModelName(modelName);
        tableInfo.setVarName(CaseUtil.tableNameToVarName(modelName));
        tableInfo.setClassName(CaseUtil.tableNameToClassName(modelName));
        //
        return tableInfo;
    }
}
